package yx.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import yx.dao.BaseDaoInt;
import yx.model.BsCourse;
import yx.vo.CourseVO;
import yx.vo.DatagridVO;

@Service("courseService")
public class CourseServiceImpl implements CourseServiceInt {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(CourseServiceImpl.class);
	
	private BaseDaoInt<BsCourse> courseDao;

	public BaseDaoInt<BsCourse> getCourseDao() {
		return courseDao;
	}

	@Autowired
	public void setCourseDao(BaseDaoInt<BsCourse> courseDao) {
		this.courseDao = courseDao;
	}

	@Override
	public CourseVO save(CourseVO course) {
		BsCourse t = new BsCourse();
		BeanUtils.copyProperties(course, t);
		courseDao.save(t);
		BeanUtils.copyProperties(t, course);
		return course;
	}

	@Override
	public DatagridVO datagrid(CourseVO course) {
		DatagridVO dg = new DatagridVO();
		String hql = "from BsCourse t where 1 = 1";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(course, hql, params);
		String totalHql = "select count(*) "+hql;
		hql = addOrder(course, hql);
		List<BsCourse> l = courseDao.find(hql,params,course.getPage(),course.getRows());
		List<CourseVO> nl = new ArrayList<CourseVO>();
		changeModel(l, nl);
		dg.setTotal(courseDao.count(totalHql,params));
		dg.setRows(nl);
		return dg;
	}

	private void changeModel(List<BsCourse> l, List<CourseVO> nl) {
		if(l != null && l.size() > 0){
			for(BsCourse t : l){
				CourseVO u = new CourseVO();
				BeanUtils.copyProperties(t, u);
				nl.add(u);
			}
		}
	}

	private String addOrder(CourseVO course, String hql) {
		if(course.getSort() != null || course.getOrder() != null){
			hql += " order by "+course.getSort()+" " + course.getOrder();
		}
		return hql;
	}

	private String addWhere(CourseVO course, String hql, Map<String, Object> params) {
		if(course.getCourseName() != null && !"".equals(course.getCourseName().trim())){
			hql += " and t.courseName like :courseName";
			params.put("courseName", "%%"+course.getCourseName().trim()+"%%");
		}
		if(course.getTeacherName() != null && !"".equals(course.getTeacherName().trim())){
			hql += " and t.teacherName like :teacherName";
			params.put("teacherName", "%%"+course.getTeacherName().trim()+"%%");
		}
		return hql;
	}

	@Override
	public void remove(String ids) {
		String[] nids = ids.split(",");
		String hql = "delete BsCourse t where t.id in (";
		for(int i=0;i<nids.length;i++){
			if(i>0){
				hql += ",";
			}
			hql += "'" + nids[i] + "'";
		}
		hql += ")";
		courseDao.executeHql(hql);
	}
	
	@Override
	public CourseVO edit(CourseVO course) {
		BsCourse t = courseDao.get(BsCourse.class, course.getId());
		BeanUtils.copyProperties(course, t);
		return course;
	}

}
